package com.example.shoppingmallclient.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.vo.ProductListVo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */

@SuppressWarnings("all")
public class ProductListAdapter extends BaseAdapter {

    private List<ProductListVo> productList = new ArrayList<>();
    private int layoutResId;
    private Context context;

    private ImageView imageView;
    private TextView tvName;
    private TextView tvPrice;
    private View itemlist;

    public ProductListAdapter(List<ProductListVo> productList, int layoutResId, Context context) {
        this.productList = productList;
        this.layoutResId = layoutResId;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(productList != null){
            return productList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(productList != null){
            return productList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            //加载item布局文件
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(layoutResId, null, false);
        }

        //在布局文件中显示数据

        //获取控件
        imageView = convertView.findViewById(R.id.iv_pic);
        tvName = convertView.findViewById(R.id.tv_name);
        tvPrice = convertView.findViewById(R.id.tv_price);
        itemlist = convertView.findViewById(R.id.item_list);


        //设置控件显示的内容

        Bitmap bitmap = productList.get(position).getImage();
        imageView.setImageBitmap(bitmap);
        String baseInformation = productList.get(position).getBaseInformation();
        tvName.setText(baseInformation);
        String price = String.valueOf(productList.get(position).getPrice());
        tvPrice.setText("￥" + price);


        return convertView;
    }

}
